package com.kindac.together.widget;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.kindac.together.av.AutoParallaxBackgroundExample;
import com.kindac.together.common.Kin;
import com.kindac.together.db.KinDB;
import com.kindac.together.service.HeadService;
import com.kindac.together.uitl.Utils;

/**
 * @author Kindac
 * @time 2011-6-15 下午11:15:16
 */
public class Together extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        // this.startActivity(new Intent(this, AutoParallaxBackgroundExample.class));
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        Kin.curAct = this;

        setContentView(createView());

        // 背景音乐
        startService(new Intent(Together.this, HeadService.class).setAction(Kin.MUSIC));
    }

    PageWidget v;

    //
    // 加载翻页
    //
    private PageWidget createView() {

        v = new PageWidget(this) {

            public void changed() {

                changeBitmap(isRight);

            }

            @Override
            public void startEngine() {

                // 释放
                Kin.bitMapCache.clear();
                Kin.bitMapCache = null;
                System.gc();

                Bitmap bt = Bitmap.createBitmap(Kin.mWidth, Kin.mHeight, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bt);
                canvas.drawColor(Color.BLACK);

                v.setBitmaps(bt, null, bt);
                postInvalidate();

                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        startActivity(new Intent(Together.this, AutoParallaxBackgroundExample.class));

                        finish();
                    }

                }, 0);

            }

            @Override
            public void startKindac(int num) {

                if (kindac == Kin.lol) {

                    kindac = 0;

                    isTuring = false;

                    if (Kin.curPage == Kin.page - 1) {
                        // 你成功了 更新数据库
                        KinDB db = KinDB.getInstance(Together.this);
                        db.updateConfigs(Together.this, 0, 0, 1, 0, -1);

                        isTuring = false;

                        Intent viewIntent = new Intent(Together.this, LolActivity.class);
                        startActivity(viewIntent);
                    } else {
                        // 你成功了 更新数据库
                        KinDB db = KinDB.getInstance(Together.this);
                        db.updateConfigs(Together.this, 0, 1, 0, 0, -1);

                        // 随机
                        int i = Utils.getRandom(Kin.lolString.length);
                        Utils.MessageBox(Together.this, getResources().getString(Kin.lolString[i]));
                    }
                }

            }

        };

        try {
            v.setBitmaps(Kin.bitMapCache.get(0), null, Kin.bitMapCache.get(1));
        } catch (Exception e) {
            Log.e(Together.class.getName(), " load pic error!! ");
        }

        return v;
    }

    private void changeBitmap(boolean isRight) {

        //
        // 翻页处理
        //
        try {
            if (isRight) {
                //
                // 每次后翻 页码自-1
                //
                Kin.curPage--;

            }
            v.setBitmaps(Kin.bitMapCache.get(Kin.curPage), null, Kin.bitMapCache.get(Kin.curPage + 1));
        } catch (Exception e) {
            Log.e(Together.class.getName(), " load pic error!! ");
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Utils.exitApp(Together.this);
        }
        return false;
    }
}
